﻿using CountTools;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraTreeList.Nodes;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;

namespace CountTools
{
    public partial class CodeNameRuleForm : Form
    {
        private List<string> elementTypes;
        public string saves2 = null;
        private string path;

        public CodeNameRuleForm()
        {
            InitializeComponent();
        }

        public CodeNameRuleForm(List<string> elementTypes, string path)
        {
            this.elementTypes = elementTypes;
            this.path = path;
            InitializeComponent();
        }


        private void CodeNameRuleForm_Load(object sender, EventArgs e)
        {
            treeList1.Enabled = true;

            //编码
            TreeListNode nodeA = treeList1.Nodes.Add("A土石方工程", "");
            TreeListNode nodeB = treeList1.Nodes.Add("B地基处理与边坡支护工程", "");
            TreeListNode nodeC = treeList1.Nodes.Add("C桩基工程", "");
            TreeListNode nodeD = treeList1.Nodes.Add("D砌筑工程", "");
            TreeListNode nodeE = treeList1.Nodes.Add("E混凝土及钢筋混凝土工程", "");
            TreeListNode nodeF = treeList1.Nodes.Add("F金属结构工程", "");
            TreeListNode nodeG = treeList1.Nodes.Add("G木结构工程", "");
            TreeListNode nodeH = treeList1.Nodes.Add("H门窗工程", "");
            TreeListNode nodeJ = treeList1.Nodes.Add("J屋面及防水工程", "");


            XmlDocument xml = new XmlDocument();
            if (File.Exists(path))
            {
                xml.Load(path);
            }
            else
            {
                string dllPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                string xmlPath = Path.Combine(dllPath, "工程量计算规范.xml");
                xml.Load(xmlPath);
            }

            foreach (string elementType in elementTypes)
            {
                string projectName = "";
                string nodeName = "";
                switch (elementType)
                {
                    case "柱":
                        projectName = "E混凝土及钢筋混凝土工程";
                        nodeName = "E.2现浇混凝土柱";
                        break;
                    case "梁":
                        projectName = "E混凝土及钢筋混凝土工程";
                        nodeName = "E.3现浇混凝土梁";
                        break;
                    case "墙":
                        projectName = "E混凝土及钢筋混凝土工程";
                        nodeName = "E.4现浇混凝土墙";
                        break;
                    case "板":
                        projectName = "E混凝土及钢筋混凝土工程";
                        nodeName = "E.5现浇混凝土板";
                        break;
                    case "钢筋":
                        projectName = "E混凝土及钢筋混凝土工程";
                        nodeName = "E.15钢筋工程";
                        break;
                }



                TreeListNode node1 = nodeE.Nodes.Add(nodeName, "");
                XmlNode childNode = xml.SelectSingleNode("工程量计算规范").SelectSingleNode(projectName).SelectSingleNode(nodeName);
                XmlNodeList childNodeList = childNode.ChildNodes;
                foreach (XmlNode child2Node in childNodeList)
                {
                    string countName = child2Node.Name;
                    string countCode = child2Node.SelectSingleNode("项目编码").InnerText;
                    string keyWords = child2Node.SelectSingleNode("关键字").InnerText;
                    node1.Nodes.Add(countCode, countName, keyWords);
                }
                node1.ExpandAll();
            }
            nodeE.ExpandAll();
        }

        private void treeList1_DoubleClick(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(treeList1.FocusedNode.GetDisplayText(treeListColumn3)) && treeList1.FocusedColumn.FieldName == "族类型关键字")
            {
                string codeName = treeList1.FocusedNode.GetDisplayText(treeListColumn2);
                string text = treeList1.FocusedNode.GetDisplayText(treeListColumn3);
                string[] Text = text.Split(',');

                ChangeKeyWordForm f3 = new ChangeKeyWordForm(this);
                f3.label1.Text = f3.label1.Text + codeName;
                for (int i = 0; i < Text.Length; i++)
                {
                    f3.richTextBox1.Text += Text[i] + "\t\n";
                }
                f3.ShowDialog();
                if (f3.DialogResult == DialogResult.OK)
                {
                    treeList1.FocusedNode.SetValue(treeListColumn3, saves2.TrimEnd(','));
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (!File.Exists(path))
            {
                string dllPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                string xmlPath = Path.Combine(dllPath, "工程量计算规范.xml");
                File.Copy(xmlPath, path);
            }
            XmlDocument xml = new XmlDocument();
            xml.Load(path);

            var nodes = treeList1.Nodes;
            for (int i = 0; i < nodes.Count; i++)
            {
                if (nodes[i].Nodes.Count > 0)
                {
                    string projectName = nodes[i].GetDisplayText(treeListColumn1);
                    var childNodes = nodes[i].Nodes;
                    {
                        for (int j = 0; j < childNodes.Count; j++)
                        {
                            string nodeName = childNodes[j].GetDisplayText(treeListColumn1); ;
                            var childChildNodes = childNodes[j].Nodes;
                            for (int k = 0; k < childChildNodes.Count; k++)
                            {
                                string codeName = childChildNodes[k].GetDisplayText(treeListColumn2);
                                string keyWords = childChildNodes[k].GetDisplayText(treeListColumn3);
                                XmlNode childNode = xml.SelectSingleNode("工程量计算规范").SelectSingleNode(projectName).SelectSingleNode(nodeName).SelectSingleNode(codeName).SelectSingleNode("关键字");
                                childNode.InnerText = keyWords;
                            }
                        }
                    }
                }
            }
            xml.Save(path);
            MessageBox.Show("保存规则成功！！");
        }

        private void button2_Click(object sender, EventArgs e)
        {

            List<string> list = new List<string>();
            var nodes = treeList1.Nodes;
            for (int i = 0; i < nodes.Count; i++)
            {
                if (nodes[i].Nodes.Count > 0)
                {
                    string projectName = nodes[i].GetDisplayText(treeListColumn1);
                    var childNodes = nodes[i].Nodes;
                    {
                        for (int j = 0; j < childNodes.Count; j++)
                        {
                            string nodeName = childNodes[j].GetDisplayText(treeListColumn1); ;
                            var childChildNodes = childNodes[j].Nodes;
                            for (int k = 0; k < childChildNodes.Count; k++)
                            {
                                string code = childChildNodes[k].GetDisplayText(treeListColumn1);
                                string codeName = childChildNodes[k].GetDisplayText(treeListColumn2);
                                string keyWords = childChildNodes[k].GetDisplayText(treeListColumn3);
                                list.Add(code + "-" + codeName + "-" + keyWords);
                            }
                        }
                    }
                }
            }

            CodeList = list;


            this.Close();
        }

        public List<string> CodeList { get; internal set; }

        private void treeList1_CustomDrawNodeCell(object sender, DevExpress.XtraTreeList.CustomDrawNodeCellEventArgs e)
        {
            if (e.Column == treeListColumn3)
            {
                if (e.CellValue == null)
                {
                    e.Appearance.BackColor = System.Drawing.Color.White;
                    e.Appearance.Options.UseBackColor = true;
                }
            }
        }
    }


}
